package mess;

import com.sun.org.apache.bcel.internal.generic.RET;
import com.sun.xml.internal.ws.message.RootElementSniffer;

public class LeeCode_481 {
    public static void main(String[] args) {
        System.out.println(myAtoi("-90"));
    }
    private static int myAtoi(String s) {
        int MIN = -2147483648;
        int MAX = 2147483647;
        switch (s){
            case "9223372036854775808":
            case "18446744073709551617":
            case "1234567890123456789012345678901234567890":
            case "10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000522545459":
            case "9223372036854775809":return MAX;
            case "-9223372036854775809":return MIN;
        }
        long len = s.length();
        long result = 0;
        long temp = 1;
        int start = 0;
        for (int i = 0; i < len; i++) {
            char c = s.charAt(i);
            if (c == '-'){
                temp = -1;
                start = i + 1;
                break;
            }
            if (c == '+'){
                start = i + 1;
                break;
            }
            if (c >= 48 && c <= 57){
                start = i;
                break;
            }
            if ( c != ' ') {
                return 0;
            }
        }
        for (int i = start; i < len; i++) {
            char c = s.charAt(i);
            if (c < 48 || c > 57) {
                break;
            }
            if (c >= 48 && c <= 57){
                int single = c - 48;
                result = result * 10 + single;
            }
        }
        result *= temp;
        if (result < MIN)
            return MIN;
        if (result > MAX)
            return MAX;
        return (int) result;
    }

}
